from jsmodel_bistable_feedback_switch_server import *
from pylab import plot
from pylab import show
from pylab import legend
from pylab import figure
from pylab import semilogy

import datetime
import os
fname = (sys.argv[0].split('.')[0] + "/"+ "".join(str(datetime.datetime.now()).split())).replace(':','').replace('.','').replace('-','') + "_" + str(sys.argv[0]) + "_log.txt"
pname = (sys.argv[0].split('.')[0] + "/"+ "".join(str(datetime.datetime.now()).split())).replace(':','').replace('.','').replace('-','') + "_" + str(sys.argv[0]) + "_plot"
print datetime.datetime.now()
fp = open(fname, 'w')

#Testiramo kolicino vhodnih plasmidov in gledamo zacetno stanje, v katero se prelevi. Zaradi vecje robustnosti kontrolirano po dva in dva
#Konfiguracija za preklop
#Koliko vec enega plasmida je potrebno, da pridemo v njegovo stanje?


BIF_P1 = linspace(1,500,150)
BIF_P2 = linspace(1,500,150)


fa = figure()
i = 1
r_old = 0;
for pp1 in BIF_P1:
  for pp2 in BIF_P2:
 
    
    t = 16000
    p1 = pp1
    p2 = pp2
    p3 = pp1
    p4 = pp2
    p5 = 200
    pcs  = [8000]
    pchl = 0
    pcd  = [2000]
    rms  = [3000]
    rmhl = 0
    rmd  = [2000]
    thresh = 1e2
 
    switch = BistableFeedbackSwitch(True, t, p1, p2, p3, p4, p5, pcs, pchl, pcd, rms, rmhl, rmd, thresh)
    r = switch.run()
            
    if not r == r_old:
      lstring = "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13} *\n"
      lstring = lstring.format(t, p1, p2, p3, p4, p5, pcs, pchl, pcd, rms, rmhl, rmd, thresh, r)
    else:
      lstring = "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13} \n"
      lstring = lstring.format(t, p1, p2, p3, p4, p5, pcs, pchl, pcd, rms, rmhl, rmd, thresh, r)
    fp.write(lstring)
    
    r_old = r;
    
    i = i + 1
    if i % 1000 == 0:
      print i
    
    if r == 0:
      plot(p1,p2, 'g.', label='BISTABLE')
    elif r == 1:
      plot(p1,p2, 'r.', label='MONOSTATE 57')
    elif r == 2:
      plot(p1,p2, 'b.', label='MONOSTATE 97')
    elif r == 3:
      plot(p1,p2, 'y.', label='AMBIGOUS')
    elif r == -1:
      plot(p1,p2, 'k.', label='NOT VALID')
    

fp.close()
xlabel("Plasmid #1,3  dosage")  
ylabel("Plasmid #2,4 dosage")
savefig(pname+".png", format ='png')
savefig(pname+".svg", format ='svg')
print datetime.datetime.now()
quit()

